import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    redirect: '/main',
  },
  {
    path: '/register',
    name: 'register',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "login" */ '../views/register/index.vue')
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "login" */ '../views/login/index.vue')
  },
  {
    path: '/main',
    name: 'main',
    meta:{
      requireAuth: true
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/Main.vue'),
    children:[
      {
        path: 'home',
        name: 'Home',
        meta:{
          requireAuth: true
        },
        component: () => import('../views/home.vue')
      },
      {
        path: 'personal',
        name: 'Personal',
        meta:{
          requireAuth: true
        },
        component: () => import('../views/user/personal.vue')
      },
      {
        path: 'logs',
        name: 'Logs',
        meta:{
          requireAuth: true
        },
        component: () => import('../views/log/Logs.vue')
      },
      {
        path: 'users',
        name: 'Users',
        meta:{
          requireAuth: true
        },
        component: () => import('../views/user/allUsers.vue')
      },
      {
        path: 'roles',
        name: 'Roles',
        meta:{
          requireAuth: true
        },
        component: () => import('../views/user/roles.vue')
      },
      {
        path: 'news',
        name: 'News',
        meta:{
          requireAuth: true
        },
        component: () => import('../views/user/news.vue'),
      },
      {
        path: 'news-detail/:id',
        // params: {id},
        name: 'News-detail',
        meta:{
          requireAuth: true
        },
        component: () => import('../views/user/newsDetail.vue')
      },
      {
        path: 'tasks',
        name: 'Tasks',
        meta:{
          requireAuth: true
        },
        component: () => import('../views/task/tasks.vue')
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

// const router = new VueRouter({
//   routes
// })

const createRouter = () => new VueRouter({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: routes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
